/* 
 * File:   Animation.cpp
 * Author: agu
 * 
 * Created on 24 de mayo de 2014, 18:51
 */

#include "Animation.h"
#define UP "00007|ARRIBA"
#define DOWN "00006|ABAJO"
#define RIGHT "00008|DERECHA"
#define LEFT "00010|IZQUIERDA"

Animation::Animation(SDL_Renderer* renderer, const string path, int x, int y) :
texture(renderer, path, x, y) {
    //Velocidad de movimiento de la animacion
    this->vx = this->vy = 0.0;
    //para manejar las teclas
    this->keys = SDL_GetKeyboardState(NULL);
    //Set sprite clips
    this->frames = 3; //Cambiarlo
    int sizeFrame = 32; //Cambiarlo
    //Ver como setearlo por cada animacion
    for (int i = 0; i < this->frames; i++) {
        SDL_Rect pos;
        pos.x = i*sizeFrame;
        pos.y = 0;
        pos.w = sizeFrame;
        pos.h = sizeFrame;
        this->spritesDown.push_back(pos);
    }

    for (int i = 0; i < this->frames; i++) {
        SDL_Rect pos;
        pos.x = i*sizeFrame;
        pos.y = sizeFrame;
        pos.w = sizeFrame;
        pos.h = sizeFrame;
        this->spritesLeft.push_back(pos);
    }

    for (int i = 0; i < this->frames; i++) {
        SDL_Rect pos;
        pos.x = i*sizeFrame;
        pos.y = sizeFrame * 2;
        pos.w = sizeFrame;
        pos.h = sizeFrame;
        this->spritesRight.push_back(pos);
    }


    for (int i = 0; i < this->frames; i++) {
        SDL_Rect pos;
        pos.x = i*sizeFrame;
        pos.y = sizeFrame*3;
        pos.w = sizeFrame;
        pos.h = sizeFrame;
        this->spritesUp.push_back(pos);
    }
    this->currentSprite = this->spritesDown;
    this->currentFrame = 0;
}

Animation::~Animation() {
}

void Animation::render() {
    
    //TODO: Completar con la verificacion de que la velocidad es nula
    //Entonces se tiene que dibujar la misma imagen y no un sprite
    //este render seria el de Texture
    Uint32 ticks= SDL_GetTicks();
    this->currentFrame=(ticks/100)%this->frames;
    this->texture.render(this->texture.getX(), this->texture.getY(),
            &this->currentSprite[this->currentFrame]);
    this->currentFrame++;
}

void Animation::updateMovement() {
    //movimientos verticales

    if (this->keys[SDL_SCANCODE_UP]) {
        this->vy = -1;
        this->currentSprite = this->spritesUp;
        this->keyName = UP;
    } else if (this->keys[SDL_SCANCODE_DOWN]) {
        this->vy = 1;
        this->currentSprite = this->spritesDown;
        this->keyName = DOWN;
    } else {
        this->vy = 0;
    }
    //movimientos horizontales
    if (this->keys[SDL_SCANCODE_LEFT]) {
        this->vx = -1;
        this->currentSprite = this->spritesLeft;
        this->keyName = LEFT;
    } else if (this->keys[SDL_SCANCODE_RIGHT]) {
        this->vx = 1;
        this->currentSprite = this->spritesRight;
        this->keyName = RIGHT;
    } else {
        this->vx = 0;
    }
//    //Nueva posicion
//    int x = this->texture.getX();
//    int y = this->texture.getY();
//
//    x += this->vx;
//    y += this->vy;
//    this->texture.setPosition(x, y);

}

void Animation::updatePosition(){
        //Nueva posicion
    int new_x = this->texture.getX();
    int new_y = this->texture.getY();

    new_x += this->vx;
    new_y += this->vy;
    this->texture.setPosition(new_x, new_y);
}

string Animation::getKeyName(){
    return this->keyName;
}
